home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / progut~1 / proff.zoo / proff01.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-04  |  11.4 KB  |  756 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include "proff.h"
  4. #include "debug.h"
  5.  
  6. #define brk brrk
  7.  
  8. /*
  9.  * bold - bold face or overstrike a line
  10.  *
  11.  */
  12. bold(buf,tbuf,size)
  13. char buf[];
  14. char tbuf[];
  15. int  size;
  16. {
  17.     int  i,j;
  18.     dprintf("bold  ");
  19.  
  20.     j = 0;
  21.     for (i = 0; buf[i] != '\n' && j < size - 2; i++) {
  22.         tbuf[j] = buf[i];
  23.         j++;
  24.         if (buf[i] != ' ' && buf[i] != '\t' &&
  25.             buf[i] != BACKSPACE ) {
  26.             tbuf[j] = BACKSPACE;
  27.             tbuf[j+1] = tbuf[j-1];
  28.             j += 2;
  29.         }
  30.     }
  31.     tbuf[j] = '\n';
  32.     tbuf[j+1] = '\0';
  33.     strcpy(buf,tbuf);
  34. }
  35.  
  36. /*
  37.  * brk - end current filled line
  38.  *
  39.  */
  40. brk()
  41. {
  42.     dprintf("brk  ");
  43.     if (outp > 0) {
  44.         outbuf[outp] = '\n';
  45.         outbuf[outp+1] = EOS;
  46.         put(outbuf);
  47.     }
  48.     outp = 0;
  49.     outw = 0;
  50.     outwds = 0;
  51. }
  52.  
  53. /*
  54.  * center - center a line by setting tival
  55.  *
  56.  */
  57. center(buf)
  58. char buf[];
  59. {
  60.     int    i;
  61.  
  62.     dprintf("center  ");
  63.  
  64.     i = (rmval + tival - width(buf)) / 2;
  65.     tival = (i > 0) ? i : 0;
  66. }
  67.  
  68. /*
  69.  * doroff - format text in file fp
  70.  *
  71.  */
  72. doroff(fp)
  73. FILE *fp;
  74. {
  75.     char inbuf[INSIZE];
  76.  
  77.     infile[0] = fp;
  78.     for (level = 0; level > -1; level--) {
  79.  
  80. #ifdef DEBUG
  81. printf("doroff: level %d FileP %ld\n",level, infile[level]);
  82. #endif
  83.  
  84.         while (ngetln(inbuf, infile[level]) != EOF) {
  85.             if (inbuf[0] == cchar)    /* a command */
  86.                 command(inbuf);
  87.             else {
  88. #ifdef rainbow
  89.                 if (biosb(2))
  90.                     exit(0);
  91. #endif
  92.                 text(inbuf);
  93.                 p_txtlines++;
  94.             }
  95.         }
  96. #ifdef DEBUG
  97. printf("doroff: Got EOF level %d FileP %ld\n",level, infile[level]);
  98. #endif
  99.  
  100.         if (level > 0 && infile[level] != NULL) {
  101.             fclose(infile[level]);
  102.             infile[level] = NULL;
  103.             if (verbose)
  104.                 fprintf(stderr,"       done.\n");
  105.         }
  106.     }
  107. }
  108.  
  109. /*
  110.  * gettl - copy title from buf to ttl
  111.  *
  112.  * modifies lim
  113.  */
  114. gettl(buf,ttl,lim)
  115. char *buf;
  116. char *ttl;
  117. int lim[];
  118. {
  119.     while (!isspace(*buf))
  120.         buf++;
  121.     while (isspace(*buf))
  122.         buf++;
  123.     strcpy(ttl,buf);
  124.     lim[0] = inval;
  125.     lim[1] = rmval;
  126. }
  127.  
  128. /*
  129.  * getwrb - get a word INCLUDING the trailing blanks
  130.  *
  131.  */
  132. int
  133. getwrb(in,i,out)
  134. char in[];
  135. char out[];
  136. int *i;
  137. {
  138.     int j,k;
  139.     dprintf("getval  ");
  140.     k = *i;
  141.     j = 0;
  142.     while (in[k] != EOS && in[k] != ' ' &&
  143.         in[k] != '\t' && in[k] != '\n') {
  144.         out[j] = in[k];
  145.         k++;
  146.         j++;
  147.     }
  148.     while (in[k] == ' ') {
  149.         out[j] = ' ';
  150.         k++;
  151.         j++;
  152.     }
  153.     *i = k;
  154.     out[j] = EOS;
  155.     return(j);
  156. }
  157.  
  158.  
  159. /*
  160.  * gfield - get next tab or title field
  161.  *
  162.  */
  163. int
  164. gfield(buf, i, n, temp, delim)
  165. char buf[];
  166. int *i;
  167. int n;
  168. char temp[];
  169. char delim;
  170. {
  171.     int j,k;
  172.  
  173.     dprintf("gfield  ");
  174.     j = 0;
  175.     k = *i;
  176.     if (n > 0) {
  177.         if (buf[k] == delim)
  178.             k++;
  179.         while (buf[k] != delim && buf[k] != EOS && buf[k] != '\n' &&
  180.             j <= n) {
  181.             temp[j] = buf[k];
  182.             j++;
  183.             k++;
  184.         }
  185.     }
  186.     temp[j] = EOS;
  187.     while (buf[k] != delim && buf[k] != EOS && buf[k] != '\n')
  188.         k++;
  189.     *i = k;
  190.     return(j);
  191. }
  192.  
  193. /*
  194.  * jcopy - scopy without copying EOS
  195.  *
  196.  */
  197. jcopy(from, i, to, j)
  198. char from[];
  199. char to[];
  200. int i;
  201. int j;
  202. {
  203.     int k1, k2;
  204.     dprintf("jcopy  ");
  205.  
  206.     k1 = i;
  207.     k2 = j;
  208.     while (from[k1] != EOS) {
  209.         to[k2] = from[k1];
  210.         k1++;
  211.         k2++;
  212.     }
  213. }
  214.  
  215. /*
  216.  * justfy - justifies string in its tab column
  217.  * */
  218. justfy(in, left, right, type, out)
  219. char in[];
  220. char out[];
  221. int left;
  222. int right;
  223. int type;
  224. {
  225.     int j,k, n;
  226.  
  227.     dprintf("justfy  ");
  228.     n = width(in);
  229.     if (type == RIGHT)
  230.         jcopy(in, 0, out, right-n);
  231.     else if (type == CENTER) {
  232.         k = (right+left-n) / 2;
  233.         j = (k > left) ? k : left;
  234.         jcopy(in, 0, out, j);
  235.     }
  236.     else 
  237.         jcopy(in, 0, out, left);
  238. }
  239.  
  240. /*
  241.  * leadbl - delete leading blanks, set tival
  242.  *
  243.  */
  244. leadbl(buf)
  245. char buf[];
  246. {
  247.     int i, j;
  248.  
  249.     dprintf("leadbl  ");
  250.     brk();
  251.     for (i = 0; buf[i] == ' '; i++)     /* find 1st non-blank */
  252.         ;
  253.     if (buf[i] != '\n')
  254.         if (autopar) {
  255.             put("\n");    /* blank line */
  256.             tival = inval + autoprv;
  257.         }
  258.         else
  259.             tival = inval + i;        /* ??????????? */
  260.     for (j = 0; buf[i] != EOS; j++) {   /* move line to left */
  261.         buf[j] = buf[i];
  262.         i++;
  263.     }
  264.     buf[j] = EOS;
  265. }
  266.  
  267. /*
  268.  * ngetln - get next line from f into line
  269.  *
  270.  */
  271. int
  272. ngetln(line, f)
  273. char line[];
  274. FILE *f;
  275. {
  276.     int c, i;
  277.  
  278. #ifdef DEBUG
  279.     printf("ngetln+++: bp %d\n",bp);
  280. #endif
  281.  
  282.     for (i = 0; (c = (bp >= 0) ? buf[bp--] : getc(f)) != EOF; ) {
  283.         if (i < MAXLINE - 1) {
  284.             line[i++] = (char) c;
  285. #ifdef DEBUG
  286.     printf("ngetln++: i %d c %c %d\n",i,c,c);
  287. #endif
  288.         }
  289.         if (c == '\n' || c == '\r')
  290.             break;
  291.     }
  292.     line[i] = EOS;
  293.     if (i == 0 && c == EOF)
  294.         i = EOF;
  295. #ifdef DEBUG
  296.     printf("ngetln: %s (%d, %d) (line)\n",line,(int)strlen(line),i);
  297. #endif
  298.     return(i);
  299. }
  300.  
  301. /*
  302.  * pbstr - push string back onto input
  303.  *
  304.  */
  305. pbstr(in)
  306. char in[];
  307. {
  308.  
  309.     int i;
  310.  
  311.     dprintf("pbstr  ");
  312.     for (i = (int)strlen(in) - 1; i >= 0; i--)
  313.         putbak(in[i]);
  314. }
  315.  
  316. /*
  317.  * pfoot - put out page footer
  318.  *
  319.  */
  320. pfoot()
  321. {
  322.  
  323.     dprintf("pfoot  ");
  324.     skipl(m3val);
  325.     if (m4val > 0) {
  326.         if (curpag % 2 == 1)
  327.             puttl(efoot, eflim, curpag);
  328.         else
  329.             puttl(ofoot, oflim, curpag);
  330.     }
  331.     if (print == YES)        /* flush the page */
  332.     {
  333.         putchar(PAGEJECT);    /* ...          */
  334.         p_outpages++;
  335.         if (stopx > 0)        /* -s, so flush ^L*/
  336.             putchar('\n');
  337.     }
  338. }
  339.  
  340. /*
  341.  * phead - put out page header
  342.  *
  343.  */
  344. phead()
  345. {
  346.     dprintf("phead  ");
  347.  
  348.     curpag = newpag;
  349.     if (curpag >= frstpg && curpag <= lastpg)
  350.         print = YES;
  351.     else 
  352.         print = NO;
  353.     if(stopx > 0 && print == YES)
  354.         prmpt(&stopx);
  355.     newpag++;
  356.     if (m1val > 0) {
  357.         skipl(m1val-1);
  358.         if (curpag % 2 == 0)
  359.             puttl(ehead, ehlim, curpag);
  360.         else
  361.             puttl(ohead, ohlim, curpag);
  362.     }
  363.     skipl(m2val);
  364.     lineno = m1val + m2val + 1;
  365. }
  366.  
  367. /*
  368.  * prmpt - pause for paper insertion
  369.  * prompt if i == 1; increment i
  370.  *
  371.  */
  372. prmpt(i)
  373. int *i;
  374. {
  375.     int junk,j;
  376.     static char bellst[2] = { BEL, EOS};
  377.  
  378.     dprintf("prmpt  ");
  379.     j = *i;
  380.     if (j == 1)
  381. #ifdef rainbow
  382.         printf("%s\033[7minsert paper and type return\033[0m ",bellst);
  383. #else
  384.         printf("%sinsert paper and type return ",bellst);
  385. #endif
  386.     else
  387.         printf(bellst);
  388.     junk = getchar();
  389.     *i = ++j;
  390. }
  391.  
  392. /*
  393.  * Put - put out line with proper spacing and indenting
  394.  *
  395.  */
  396. put(buf)
  397. char buf[];
  398. {
  399.     register int i;
  400.     dprintf("put  ");
  401.     if (lineno == 0 || lineno > bottom)
  402.         phead();
  403.  
  404.     if ( print == YES ) {
  405.         if (buf[0] == '\n') {    /* empty line.. */
  406.             putchar('\n');
  407.             p_outlines++;
  408.         }
  409.         else {
  410.             for ( i = 1 ; i <= offset ; i++ ) /* page offset */
  411.                 putchar(' ');
  412.             for ( i = 1 ; i <= tival ; i++ )  /* indenting   */
  413.                 putchar(' ');
  414.  
  415.             while (*buf != '\0') {
  416.                 putchar(*buf);
  417.                 buf++;
  418.             }
  419.             p_outlines++;
  420.         }
  421.     }
  422.  
  423.     tival = inval;
  424.     skipl(((lsval-1 < bottom-lineno) ? lsval-1 : bottom-lineno));
  425.     lineno += lsval;
  426.  
  427.     if (lineno > bottom)
  428.         pfoot();
  429.  
  430. }
  431.  
  432. /*
  433.  * putbak - push character back onto input
  434.  *
  435.  */
  436. putbak(c)
  437. char c;
  438. {
  439.     dprintf("putbak  ");
  440.  
  441.     bp++;
  442.     if (bp > BUFSIZE)
  443.         error("too many characters pushed back.\n");
  444.     buf[bp] = c;
  445. }
  446.  
  447.  
  448. /*
  449.  * puttl - put out title line with optional page number & date
  450.  * 
  451.  */
  452. puttl(buf, lim, pageno)
  453. char buf[];
  454. int lim[];
  455. int pageno;
  456. {
  457.     char chars[9],cdate[27];
  458.     char rmstr[MAXTOK];
  459.     char delim;
  460.     char *tp;
  461.     int j;
  462.     int nc, n, i, left, right, ncd;
  463.  
  464.     dprintf("puttl  ");
  465.     if (print == NO)
  466.         return;
  467.     left = lim[0];    /* no more +1 here */
  468.     right = lim[1]; /* no more +1 here */
  469.     nc = itoc(pageno, chars, MAXCHARS);
  470.     if (roman) {
  471.         nc = cvtroman(chars,rmstr);
  472.         strcpy(chars,rmstr);
  473.     }
  474.     getnow(cdate);
  475.     ncd = (int)strlen(cdate);
  476.     i = 0;
  477.     delim = buf[i];
  478.     for (j = 0; j < right; j++)
  479.         ttl[j] = ' ';
  480.     n = 0;
  481.     do {
  482.         if (gfield(buf, &i, right-left, tbuf1, delim) > 0) {
  483.             subst(tbuf1, PAGENUM, tbuf2, chars, nc);
  484.             subst(tbuf2, CURRENTDATE, tbuf1, cdate, ncd);
  485.             justfy(tbuf1, left, right, tjust[n], ttl);
  486.         }
  487.         n++;        /* update title counter */
  488.     } 
  489.     while (buf[i] != EOS && buf[i] != '\n' && n != 3);
  490.  
  491.     for( ; right >= 1 ; right--)
  492.         if( ttl[right-1] != ' ' )
  493.             break;
  494.     ttl[right] = '\n';
  495.     ttl[right+1] = EOS;
  496.     for (i = 1; i <= offset; i++)
  497.         putchar(' ');              /* offset */
  498.     tp = ttl;
  499.     while (*tp != '\0') {
  500.         putchar(*tp);
  501.         tp++;
  502.     }
  503.     p_outlines++;
  504. }
  505.  
  506. /*
  507.  * set - set parameter and check range
  508.  *
  509.  */
  510. set(param, val, argtyp, defval, minval, maxval)
  511. int *param;
  512. int val;
  513. int argtyp;
  514. int defval;
  515. int minval;
  516. int maxval;
  517. {
  518.     int i;
  519.     dprintf("set  ");
  520.     i = *param;
  521.     if (argtyp == '\n')              /* defaulted */
  522.         i = defval;
  523.     else if (argtyp == '+')                  /* relative +*/
  524.         i += val;
  525.     else if (argtyp == '-')           /* relative -*/
  526.         i -= val;
  527.     else                           /* absolute  */
  528.     i = val;
  529.     i = (i < maxval) ? i : maxval;         /* min          */
  530.     i = (i > minval) ? i : minval;         /* max          */
  531.     *param = i;
  532. }
  533.  
  534. /*
  535. * skipl - output  n  blank lines
  536. *
  537. */
  538. skipl(n)
  539. register int n;
  540. {
  541.     register int i;
  542.  
  543.     dprintf("skip  ");
  544.     if (print == YES)
  545.         for (i = 1; i <= n; i++) {
  546.             putchar('\n');
  547.             p_outlines++;
  548.         }
  549. }
  550.  
  551. /*
  552.  * space - space  n  lines or to bottom of page
  553.  *
  554.  */
  555. space(n)
  556. int n;
  557. {
  558.  
  559.     dprintf("space  ");
  560.     brk();
  561.     if (lineno > bottom)
  562.         return;
  563.     if (lineno == 0)
  564.         phead();
  565.     skipl(((n < bottom+1-lineno) ? n : bottom+1-lineno));
  566.     lineno += n;
  567.     if (lineno > bottom)
  568.         pfoot();
  569. }
  570.  
  571. /*
  572.  * spread - spread words to justify right margin
  573.  *
  574.  */
  575. spread(buf, outp, nextra, outwds)
  576. char buf[];
  577. int outp;
  578. int nextra;
  579. int outwds;
  580. {
  581.     int dir = 0;
  582.  
  583.     register int i, j;
  584.     int nb, ne, nholes;
  585.  
  586.     dprintf("spread  ");
  587.     if (nextra <= 0 || outwds <= 1)
  588.         return;
  589.     dir = 1 - dir;   /* reverse previous direction */
  590.     ne = nextra;
  591.     nholes = outwds - 1;
  592.     if (tival != inval && nholes > 1)
  593.         nholes--;
  594.     i = outp - 1;
  595.     j = (MAXOUT-2 < i+ne) ? MAXOUT-2 : i+ne; /* leave room for '\n', EOS */
  596.     while (i < j) {
  597.         buf[j] = buf[i];
  598.         if (buf[i] == ' ' && buf[i-1] != ' ') {
  599.             if (dir == 0)
  600.                 nb = (ne-1) / nholes + 1;
  601.             else
  602.                 nb = ne / nholes;
  603.             ne -= nb;
  604.             nholes--;
  605.             for ( ; nb > 0; nb--) {
  606.                 j--;
  607.                 buf[j] = ' ';
  608.             }
  609.         }
  610.         i--;
  611.         j--;
  612.     }
  613. }
  614.  
  615. /*
  616.  * subst - substitutes a string for a specified character
  617.  *
  618.  */
  619. subst(in, chr, out, subara, n)
  620. char in[];
  621. char chr;
  622. char out[];
  623. char subara[];
  624. int n;
  625. {
  626.     register int i, j, k;
  627.  
  628.     dprintf("subst  ");
  629.     j = 0;
  630.     for (i = 0; in[i] != EOS; i++)
  631.         if (in[i] == chr)
  632.             for (k = 0; k < n; k++) {
  633.                 out[j] = subara[k];
  634.                 j++;
  635.             }
  636.         else {
  637.             out[j] = in[i];
  638.             j++;
  639.         }
  640.     out[j] = EOS;
  641. }
  642.  
  643. /*
  644.  * Text    process text lines
  645.  *
  646.  */
  647.  
  648. text(inbuf)
  649. char inbuf[];
  650. {
  651.     int    i;
  652.     register int j;
  653.     char    wrdbuf[INSIZE];
  654.  
  655.     dovar(wrdbuf,inbuf);        /*  expand variables */
  656.     strcpy(inbuf,wrdbuf);
  657.     doesc(inbuf, wrdbuf, INSIZE);    /*  expand escapes   */
  658.     dotabs(inbuf, wrdbuf, INSIZE);  /*  expand tabs      */
  659.  
  660.     if(inbuf[0] == ' ' || inbuf[0] == '\n')
  661.         leadbl(inbuf);         /* move left, set tival */
  662.     if(ulval > 0 || ULon)         /* word underlining */
  663.     {
  664.         underl(inbuf, wrdbuf, INSIZE);
  665.         ulval--;
  666.     }
  667.     if(boval > 0 || BDon)         /* boldfacing */
  668.     {
  669.         bold( inbuf, wrdbuf, INSIZE);
  670.         boval--;
  671.     }
  672.     if(ceval > 0 || CEon)         /* centering */
  673.     {
  674.         center(inbuf);
  675.         put(inbuf);
  676.         ceval--;
  677.     }
  678.     else if( inbuf[0] == '\n' )     /* all blank line */
  679.         put(inbuf);
  680.     else if( fill == NO )         /* unfilled text */
  681.         put(inbuf);
  682.     else                 /* filled text */
  683.     {
  684.         i = (int)strlen(inbuf) - 1;
  685.         inbuf[i] = ' ';
  686.         if( inbuf[i-1] == '.' )
  687.         {
  688.             i++;
  689.             inbuf[i] = ' ';
  690.         }
  691.         inbuf[i+1] = EOS;
  692.         for( i = 0 ; getwrb(inbuf, &i, wrdbuf) > 0 ; )
  693.             putwrd(wrdbuf);
  694.     }
  695. }
  696.  
  697. /*
  698.  * Underl    underline words in a line
  699.  *
  700.  */
  701. underl(buf, tbuf, size)
  702. char buf[];
  703. char tbuf[];
  704. int size;
  705. {
  706.     int i, j;
  707.  
  708.     j = 0;
  709.     for(i = 0 ; buf[i] != '\n' && j < size - 2; i++) {
  710.         if( buf[i] != ' ' && buf[i] != BACKSPACE && buf[i] != '_' ) {
  711.             tbuf[j++] = '_';
  712.             tbuf[j++] = BACKSPACE;
  713.         }
  714.         if( buf[i] == BLANK )
  715.             tbuf[j++] = ulblnk;
  716.         else
  717.             tbuf[j++] = buf[i];
  718.     }
  719.  
  720.     tbuf[j] = '\n';
  721.     tbuf[j+1] = '\0';
  722.     strcpy(buf, tbuf);
  723. }
  724.  
  725. /*
  726.  * width - compute width of character string
  727.  *
  728.  */
  729. int
  730. width(buf)
  731. char buf[];
  732. {
  733.     int k,i;
  734.  
  735.     dprintf("width  ");
  736.     k = 0;
  737.     for (i = 0; buf[i] != EOS; i++)
  738.         if (buf[i] == BACKSPACE)
  739.             k--;
  740.         else if (buf[i] >= ' ' && buf[i] <= '~')
  741.             k++;
  742.     return(k);
  743. }
  744. /*
  745.  * getnow - get the date from command line if present.
  746.  *          if not specified, prompt user for it.
  747.  *
  748.  * (stub)
  749.  */
  750. getnow(date)
  751. char date[];
  752. {
  753.     dprintf("getnow  ");
  754.     strcpy(date,"00-xxx-1900 00:00:00");
  755. }
  756.